Centos7修改iptables规则并开机永久生效两种方式 |
您所在的位置:网站首页 › linux 永久关闭防火墙 centos 7 › Centos7修改iptables规则并开机永久生效两种方式 |
相信很多使用Centos7的朋友和我一样,都是删除默认的firewalld服务,而重新安装iptables做为防火墙。这一篇文章我们一起来看看如何修改iptables规则以及如何使得修改的iptables规则永久生效。 我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。 文章目录 前提条件修改iptables的两种方式修改配置文件命令行 保存命令行修改的规则第一种方法第二种方法 总结 前提条件这篇文章的前提条件是centos7机器已经按照下面方法删除了firewalld服务,并安装了iptables。同时将iptables设置为了开机自启动 sudo systemctl stop firewalld.service sudo systemctl disable firewalld.service sudo yum -y install iptables-services sudo systemctl start iptables.service sudo systemctl enable iptables.service 修改iptables的两种方式 修改配置文件配置文件的路径为/etc/sysconfig/iptables,直接编辑这个文件,例如在该文件最上面的规则插入一条允许80端口的规则 # sample configuration for iptables service # you can edit this manually or use system-config-firewall # please do not ask us to add additional ports/services to this default configuration *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT之后如果想马上生效,需要重启iptables服务 systemctl restart iptables再查看iptables规则的时候会发现刚才的规则已经生效了,如下最上面那条 [root@k8s-master sysconfig]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80 51 3152 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 1 229 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 30 packets, 3520 bytes) pkts bytes target prot opt in out source destination之后通过reboot命令重启机器,之后查看iptables规则发现修改的规则依然生效。 这说明直接修改iptables的配置文件会永久生效。 命令行除了直接修改配置文件,也可以命令行快速修改规则。尤其是在批量用脚本修改iptables的场合命令行会更适用一些。 例如直接在命令行添加了一个允许443端口的规则 [root@k8s-master ~]# iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT [root@k8s-master ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:443 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80 535 38884 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 192 32397 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 30 packets, 1940 bytes) pkts bytes target prot opt in out source destination可以看到命令跑完就直接生效,但是命令对应的规则并没有被写到配置文件里面,所以开机以后就失效了。 那么该如何保存命令行修改的规则呢? 保存命令行修改的规则 第一种方法执行命令service iptables save,如下 [root@k8s-master ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]之后查看下配置文件,发现新的规则被写进了配置文件中,并且里面还写了是通过命令行写入的 # Generated by iptables-save v1.4.21 on Sun Apr 26 22:00:57 2020 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [4:440] -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT # Completed on Sun Apr 26 22:00:57 2020之后再重启机器就依然是生效的了。 第二种方法执行命令iptables-save > xxx写入到一个文件,开机以后执行命令iptables-restore < xxx用来恢复。 例如删除掉刚才配置文件里面的443端口那条命令,然后将完整规则保存到别的目录 [root@k8s-master sysconfig]# iptables-save > /tmp/iptables.bak [root@k8s-master sysconfig]# cat /tmp/iptables.bak # Generated by iptables-save v1.4.21 on Sun Apr 26 22:24:10 2020 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [351:44722] -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT # Completed on Sun Apr 26 22:24:10 2020之后在/etc/rc.d/rc.local中添加如下命令 /usr/sbin/iptables-restore < /tmp/iptables.bak之后给rc.local文件添加可执行权限使得可以被开机运行 chmod a+x rc.local再开机就会发现虽然配置文件没有更改,但是规则还是生效了。但是这样子有一个弊端,因为真正保存完整规则的并不是配置文件,而是另外的一个文件,如果运维人员没有记录好容易造成问题。 总结总结一下两种修改iptables规则的方法: 直接修改配置文件命令行修改规则而其中直接修改配置文件会自动永久生效,通过命令行修改规则可以用下面两种方法永久生效: service iptables saveiptables-save和iptables-restore以及开机文件/etc/rc.d/rc.local我个人比较建议的方式是,少量更改可以考虑修改配置文件,大量批量修改用命令行然后用service iptables save保存到配置文件中。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |